package org.zyx.item.controller;

import org.springframework.http.ResponseEntity;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;
import org.zyx.item.entity.Brand;
import org.zyx.item.entity.Category;
import org.zyx.item.service.BrandService;
import org.zyx.yiyou.common.entity.PageResult;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/brand")
public class BrandHandler {

    @Resource
    private BrandService brandService;

    /**
     * 分页查找品牌
     * @param key 关键字查询
     * @param page 当前页
     * @param rows 每页行数
     * @param sortBy 排序
     * @param desc 排序规则
     * @return
     */
    @GetMapping("/page")
    public ResponseEntity<PageResult> findBrandByPage(
           @RequestParam(value = "key",required = false) String key,
           @RequestParam(value = "page",defaultValue = "1") int page,
           @RequestParam(value = "rows",defaultValue = "5") int rows,
           @RequestParam(value = "sortBy",required = false) String sortBy,
           @RequestParam(value = "desc",required = false) Boolean desc
    ){

        PageResult pageResult = brandService.findBrandByPage(key,page,rows,sortBy,desc);

        if (CollectionUtils.isEmpty(pageResult.getItems())){
           return ResponseEntity.notFound().build();
        }

        return ResponseEntity.ok(pageResult);
    }

    /**
     * 新增品牌
     * @param brand  品牌对象
     * @param cids   分类数组
     */
    @PostMapping
    public ResponseEntity<Void> saveBrand(@RequestParam("cids") List<Long> cids, Brand brand){

        brandService.savaBrand(brand,cids);
        return ResponseEntity.ok().build();
    }

    /**
     * 更新品牌信息
     * @return
     */
    @PutMapping
    public ResponseEntity<Void> updBrand(@RequestParam("cids") List<Long> cids, Brand brand){
        brandService.updBrand(brand,cids);
        return ResponseEntity.ok().build();
    }

    /**
     * 通过品牌查找相关分类
     * @param bid
     * @return
     */
    @GetMapping("/bid/{bid}")
    public ResponseEntity<List<Category>> findCidsByBid(@PathVariable("bid") Long bid){

        List<Category> cids = brandService.findCategorysByBid(bid);
        if(CollectionUtils.isEmpty(cids)){
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(cids);
    }
    @DeleteMapping("/deleteBrand/{bid}")
    public ResponseEntity<Void> deleteByBid(@PathVariable("bid") Long bid){
        brandService.deleteByBid(bid);
        return ResponseEntity.ok().build();
    }

    /**
     * 根据分类id查询品牌列表
     * @param cid
     * @return
     */
    @GetMapping("/cid/{cid}")
    public ResponseEntity<List<Brand>> findBrandsByCid(@PathVariable("cid") Long cid){

        List<Brand> brands = brandService.findBrandsByCid(cid);
        if(CollectionUtils.isEmpty(brands)){
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(brands);
    }

    /**
     * 通过id查找分类
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public ResponseEntity<Brand> findBrandById(@PathVariable("id") Long id){
        Brand brand = brandService.findBrandById(id);
        if(ObjectUtils.isEmpty(brand)){
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(brand);
    }


}
